﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGSVR.Common;
using System.Data;
using System.Data.SqlClient;

namespace LGSVR.BLL.Sys
{
    public class UserBLL
    {
        #region 验证用户登录
        /// <summary>
        /// 验证用户登录
        /// </summary>
        /// <param name="strLoginName"></param>
        /// <param name="strPassword"></param>
        /// <returns></returns>
        public bool CheckLogin(string strLoginName, string strPassword, string ip)
        {

            string sql = "select * from userInfo where userID=@userID and state <>'P'";
            List<SqlParameter> lstParam = new List<SqlParameter>();
            lstParam.Add(new SqlParameter("@userID", strLoginName));
            System.Data.DataTable dt = SQLHelper.GetDataTable(sql, CommandType.Text, lstParam.ToArray());

            bool flag = true;
            if (dt.Rows.Count == 0)
            {
                flag = false;
            }
            else
            {
                if (strPassword != dt.Rows[0]["PWD"].ToString())
                {
                    flag = false;
                }
                else
                {
                    sql = "update userinfo set lastLogin='" + DateTime.Now + "' where userID=@userID and state <>'P'";
                    lstParam.Clear();
                    lstParam.Add(new SqlParameter("@userID", strLoginName));
                    SQLHelper.ExecuteNonQuery(sql, CommandType.Text, lstParam.ToArray());
                    SaveLog(strLoginName, ip, "网址登录",1);
                }
            }

            return flag;
        }
        #endregion


        public  int SaveLog(string userID, string ip, string content,int type)
        {
            List<SqlParameter> lstParam = new List<SqlParameter>();
            lstParam.Add(new SqlParameter("@userID", userID));
            lstParam.Add(new SqlParameter("@loginTime",DateTime.Now));
            lstParam.Add(new SqlParameter("@outTime",DateTime.Now));
            lstParam.Add(new SqlParameter("@ipAddress", ip));
            lstParam.Add(new SqlParameter("@Content",content));

            string str = "";
            if (type == 1)
            {
                str = "insert into userLoginLog(userID,loginTime,ipAddress,Content)"
                          + " values(@userID,@loginTime,@ipAddress,@Content)";
            }
            else
            {
                str = "insert into userLoginLog(userID,outTime,ipAddress,Content)"
                          + " values(@userID,@outTime,@ipAddress,@Content)";
            }
            return LGSVR.Common.SQLHelper.ExecuteNonQuery(str, CommandType.Text, lstParam.ToArray());
        }

        public void logOut(string userID,string ip)
        {
            SaveLog(userID, ip, "用户注销",2);
        }
       
    }
}
